/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipo.mm.web.formbeans;

import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.Globals;
import org.ipo.mm.ejb.entities.InstitutionRegion;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
import org.ipo.mm.ejb.entities.City;
import org.ipo.mm.web.util.ConstantManager;
import java.util.logging.Logger;
import org.ipo.mm.web.util.UTFEncoder;
/**
 *
 * @author makcro
 */
public class CityBean extends org.apache.struts.validator.ValidatorForm {
    private String name;
    private String zipCode;
    private String note;
    private String submit;
    private String region;
    private String operation;

    /**
     *
     */
    public CityBean() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

      

    //return super.validate(mapping, request);

        ActionErrors errors = null;
       //ActionErrors errors = super.validate(mapping, request);
        MessageResources mResources = (MessageResources) request.getAttribute(Globals.MESSAGES_KEY);
        Locale locale = (Locale) request.getSession().getAttribute(Globals.LOCALE_KEY);


        String create = mResources.getMessage(locale, "gen_create");
        String update = mResources.getMessage(locale, "gen_update");
        if (create.equals(getOperation()) || update.equals(operation)) {
            errors = super.validate(mapping, request);
        }
        return errors;
    }

    @Override
    public void reset(ActionMapping mapping, HttpServletRequest request) {


    super.reset(mapping, request);
        String method = request.getParameter("method");
        Logger.getLogger(getClass().getName()+" in reset").log(Level.INFO, "method={}",method);

        InstitutionRegion lastCreatedRegion = (InstitutionRegion) request.getAttribute(ConstantManager.REQUEST_REGION_LAST_CREATED);
        if(lastCreatedRegion != null){
            this.region = lastCreatedRegion.getId().toString();
        }
        if ("showUpdateCityForm".equals(method) || "viewCityForm".equals(method) ) {
            City city = (City) request.getSession().getAttribute("updateCity.city");
            this.setName(city.getName());
            this.setZipCode(city.getZipCode());
            this.setNote(city.getNote());
            this.setRegion(city.getRegion() == null ? "" : String.valueOf(city.getRegion().getId()));
            //this.setRegion(city.getRegion().getId().toString());
        }



    }

    /**
     * @return the name
     */
    public String getName() {
//        String name1="";
//        try {
//            if (name != null) {
//                 Logger.getLogger(CityBean.class.getName()).log(Level.INFO, null,"prije: "+name );
//                // this.name = new String(name.getBytes(ConstantManager.ENCODING_ISO88591), ConstantManager.ENCODING_UTF8);
//                name1 = new String(name.getBytes(ConstantManager.ENCODING_UTF8), ConstantManager.ENCODING_UTF8);
//
//                 Logger.getLogger(CityBean.class.getName()).log(Level.INFO, null,"nakon: "+name );
//            }
//
//            // this.name = name;
//        } catch (UnsupportedEncodingException ex) {
//            Logger.getLogger(CityBean.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        return name+" "+name1;
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
//       String n2="";
//       try {
//        String n1 = new String(name.getBytes(ConstantManager.ENCODING_UTF8), ConstantManager.ENCODING_UTF8);
//        System.out.println("iz utf to utf "+n1);
//        n2 =new String(name.getBytes(ConstantManager.ENCODING_ISO88591), ConstantManager.ENCODING_UTF8);
//        System.out.println("iz iso to utf "+n2);
//
//       } catch (UnsupportedEncodingException ex) {
//           Logger.getLogger(CityBean.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        this.name=UTFEncoder.encode(name);

        this.name=name;
    }

    /**
     * @return the zipCode
     */
    public String getZipCode() {
        return zipCode;
    }

    /**
     * @param zipCode the zipCode to set
     */
    public void setZipCode(String zipCode) {
//        try {
//            if (zipCode != null) {
//                // this.zipCode = new String(zipCode.getBytes(ConstantManager.ENCODING_ISO88591), ConstantManager.ENCODING_UTF8);
//                this.zipCode = new String(zipCode.getBytes(ConstantManager.ENCODING_UTF8), ConstantManager.ENCODING_UTF8);
//            }
//
//        } catch (UnsupportedEncodingException ex) {
//            Logger.getLogger(CityBean.class.getName()).log(Level.SEVERE, null, ex);
//        }
        this.zipCode = UTFEncoder.encode(zipCode);
    }

    /**
     * @return the note
     */
    public String getNote() {
        return note;
    }

    /**
     * @param note the note to set
     */
    public void setNote(String note) {
        this.note = note;
    }

    /**
     * @return the submit
     */
    public String getSubmit() {
        return submit;
    }

    /**
     * @param submit the submit to set
     */
    public void setSubmit(String submit) {
        this.submit = submit;
    }

     public String getRegion() {
         return region;
    }

    public void setRegion(String region) {
        Logger.getLogger(CityBean.class.getName()).log(Level.INFO, "setRegion: {0}", region);
        this.region = region;
    }
     public String getOperation() {
        return operation;
    }

    /**
     * @param operation the operation to set
     */
    public void setOperation(String operation) {
        this.operation = operation;
    }

}
